package com.kedacom.ctsp.authority.controller;

import com.kedacom.ctsp.authority.constant.AccessLoggerConstant;
import com.kedacom.ctsp.authority.entity.DataDict;
import com.kedacom.ctsp.authority.service.DataDictService;
import com.kedacom.ctsp.authority.vo.DataDictVO;
import com.kedacom.ctsp.lang.mapper.BeanMapper;
import com.kedacom.ctsp.logging.AccessLogger;
import com.kedacom.ctsp.orm.param.Param;
import com.kedacom.ctsp.orm.param.Term;
import com.kedacom.ctsp.web.controller.AbstractCrudController2;
import com.kedacom.ctsp.web.controller.message.ResponseMessage;
import com.kedacom.ctsp.web.entity.page.PagerResult;
import com.kedacom.ctsp.web.entity.param.QueryParamEntity;
import com.kedacom.ctsp.web.service.StatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ResponseBody
@Api(tags = "dataDict", description = "字典表")
@AccessLogger(AccessLoggerConstant.Module.DATA_DICT)
@RequestMapping("${ctsp.web.mappings.authority.dataDictCategory:dataDict}")
public class DataDictController extends AbstractCrudController2<DataDict, String, DataDictVO, DataDictService> {


    @Override
    public ResponseMessage<String> updateByPrimaryKey(String id, DataDictVO data) {
        return super.patchByPrimaryKey(id, data);
    }

    @GetMapping("combobox-list")
    @ApiOperation("按分类获取字典列表")
    public ResponseMessage<List<DataDictVO>> comboboxList(@RequestParam String category) {
        Param param = new Param();
        param.addTerm(Term.build(DataDict.STATUS, StatusEnum.ENABLE));
        param.addTerm(Term.build(DataDict.CATEGORY, category));
        List<DataDict> dataDictList = getService().select(param);
        List<DataDictVO> dataDictVOList = BeanMapper.deepMap(dataDictList, DataDictVO.class);
        return ResponseMessage.ok(dataDictVOList);
    }

    /**
     * 校验字典标识唯一性
     *
     * @param code
     * @param category
     * @return
     */
    @GetMapping("/unique/{code}/{category}")
    public ResponseMessage<Boolean> checkCodeUnique(@PathVariable String code, @PathVariable String category) {
        long total = getService().createQuery().where(DataDict.CODE, code).where(DataDict.CATEGORY, category).total();
        return ResponseMessage.ok(total == 0);
    }
}
